﻿CREATE PROCEDURE [acms].[Page_Put]
		@Id int,
		@VersionId INT,
		@SiteId INT,
		@ParentId INT,
		@Name VARCHAR(50),
		@DisplayName VARCHAR(50),
		@Id_Out int OUTPUT,
		@VersionId_Out int OUTPUT
AS

SELECT @Id_Out = Id from [acms].[Pages] where [Id] = @Id and [Deleted] = 0
SELECT @VersionId_Out = Id FROM [acms].[PageVersions] where Id = @VersionId AND PageId = @Id

IF @Id_Out IS NOT NULL AND @VersionId_Out IS NOT NULL
BEGIN
	UPDATE [acms].[PageVersions]
	SET [SiteId] = @SiteID,
		[ParentId] = @ParentId,
		[Name] = @Name,
		[DisplayName] = @DisplayName
	WHERE [Id] = @Id
END 
ELSE
BEGIN
	IF @Id_Out IS NULL
	BEGIN
		INSERT INTO [acms].[Pages]
			([Deleted])
		VALUES
			(0)
		SET @Id_Out = @@IDENTITY
	END
	
	Insert into [acms].[PageVersions]
	(
		[PageId],
		[SiteId],
		[ParentId],
		[Name],
		[DisplayName]
	)
	VALUES
	(
		@Id_Out,
		@SiteId,
		@ParentId,
		@Name,
		@DisplayName
	)
	
	SET @VersionId_Out = @@Identity
END

GO